<?php

namespace app\controllers;
use yii\db\Query;
use yii\web\Controller;
use yii\data\Pagination;

class ApiWorkController extends ApiBaseController {
    //职位
    
    
    //工作地点
    public function actionRegion(){
    	  $hot=(new Query())->select('id,name,lng,lat')->from('yii_region')->where(['in','id',[2,802,1965,1988,2368,934,821,2324,1828,2899,20,861]])->all();

    	$list = array(array('name'=>"A"),array('name'=>"B"),array('name'=>"C"),array('name'=>"D"),array('name'=>"E"),array('name'=>"F"),array('name'=>"G"),array('name'=>"H"),array('name'=>"I"),array('name'=>"J"),array('name'=>"K"),array('name'=>"L"),array('name'=>"M"),array('name'=>"N"),array('name'=>"O"),array('name'=>"P"),array('name'=>"Q"),array('name'=>"R"),array('name'=>"S"),array('name'=>"T"),array('name'=>"U"),array('name'=>"V"),array('name'=>"W"),array('name'=>"X"),array('name'=>"Y"),array('name'=>"Z"));

       foreach($list as $k=>$v){
       	$where['level']=2; 
       	$where['first']=$v;
       	$list[$k]['region']=(new Query())->select('id,name,lng,lat')->from('yii_region')->where($where)->all();
       }
       
      return json_encode(['hot'=>$hot,'region'=>$list]);
    	
    }


       //更新区域
     public function actionUpdateregion(){
     	 $data=\Yii::$app->request->get();
     	 $where['pid']=$data['city'];
     	 $region=(new Query())->select('id,name,lng,lat')->from('yii_region')->where($where)->all();
         $typedata['id']='';
         $typedata['name']='全部';
         array_unshift($region, $typedata);
     	 return json_encode(['region'=>$region]);
     }

    //职位首页
     public function actionIndex(){
     	$data['create_time']=1;
        $data=\Yii::$app->request->get();

        $query=(new Query())->select('id,name,city,type,region,company,experience,education,min_salary,max_salary,welfare,lng,lat')->from('yii_work');
        $where=['status'=>10]; 
        
        if($data['city']=='null'){
        $where['city']=1965;
        }else{
        //  $where['city']=1965;
        $where['city']=$this->GetIdByRegion($data['city']);	
        }
         

        $query->where($where); 
        //时间条件 
        if(!empty($data['create_time'])){
            switch ($data['create_time']) {
                case 1:
                    $time=mktime(0,0,0,date('m'),date('d'),date('Y'));
                    break;
                case 2:
                    $time=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
                    break;
                case 3:
                    $time=mktime(0,0,0,date('m'),date('d')-6,date('Y'));
                    break;
                case 4:
                    $time=mktime(0,0,0,date('m'),date('d')-13,date('Y'));
                    break;
            }
            $query->andWhere(['>','create_time',$time]);

        }
        //辖区条件
        if (!empty($data['region'])){
            $query->andWhere(['=','region',$data['region']]);
        }
        //薪资条件
        if (!empty($data['salary'])){
            $salar=explode('-',$data['salary']);
            $query->andWhere(['>=','min_salary',$salar[0]]);
            $query->andWhere(['<=','max_salary',$salar[1]]);
        }

        //条件
        if (!empty($data['type'])){
            switch ($data['type']){
                case 4:
                    $query->andWhere(['=','type',10]);
                    break;
                case 5:
                    $query->andWhere(['=','type',20]);
                    break;
            }
        }

         $pagecount=floor($query->count()/10);
         $page=$data['page']?$data['page']:0;        
         $list=$query->offset($page*10)->limit(10)->all();
        //判断公司里

        foreach ($list as $k=>$v){
            $list[$k]['city']=$this->GetRegion($v['city']);
            $list[$k]['region']=$this->GetRegion($v['region']);
            $list[$k]['experience']=$this->GetExperience($v['experience']);
            $list[$k]['education']=$this->GetEducation($v['education']);
            $list[$k]['welfare']=$this->GetWelfare($v['welfare']);
            $list[$k]['company']=$this->GetCompany($v['company']);
            if($data['lng']=='null'){
            $list[$k]['distance']=0;	
            }else{
            $list[$k]['distance']=$this->distance($v['lng'],$v['lat'],$data['lng'],$data['lat']);
            }
         
        }
        $list2=[];
        if (!empty($data['distance'])){
            foreach ($list as $k=>$v){
              if ($v['distance']<=$data['distance']){
                  array_push($list2,$list[$k]);
              }
            }
        }else{
            foreach ($list as $k=>$v){
                array_push($list2,$list[$k]);
            }
        }

        //条件
        if (!empty($data['type'])){
           switch ($data['type']){
               case 1:
                   $distance = array_column($list2,'distance');
                   array_multisort($distance,SORT_ASC,$list2);
                   break;

               case 2:
                   $distance = array_column($list2,'distance');
                   array_multisort($distance,SORT_DESC,$list2);
                   break;

               case 3:
                   $max_salary = array_column($list2,'max_salary');
                   array_multisort($max_salary,SORT_DESC,$list2);
                   break;

           }
        }

        $region=(new Query())->select('id,name,lng,lat')->from('yii_region')->where(['pid'=>$where['city']])->all();
        $typedata['id']='';
        $typedata['name']='全部';
        array_unshift($region, $typedata);
        return json_encode(['list'=>$list2,'region'=>$region,'page'=>$pagecount]);
    }


   
      
      //两个经纬度之间的距离（千米）
      public function distance($lng1,$lat1,$lng2,$lat2){
    $EARTH_RADIUS = 6378137;   //地球半径
    $RAD = pi() / 180.0;

    $radLat1 = $lat1 * $RAD;
    $radLat2 = $lat2 * $RAD;
    $a = $radLat1 - $radLat2;    // 两点纬度差
    $b = ($lng1 - $lng2) * $RAD;  // 两点经度差
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * $EARTH_RADIUS;
    $s = round($s * 10000) / 10000/1000;
    return round($s,2);
      }
        
        
      //通过城市名查ID
     public function GetIdByRegion($name){
     	$list=(new Query())->select('id')->from('yii_region')->where(['like','name',$name])->andWhere(['level'=>2])->one();
     	return $list['id'];
     	
     }

       //职位详情
     public function actionLook($id){
         $where['id']=(int)$id;
         $list=(new Query())->select('id,name,company,city,type,region,experience,education,min_salary,max_salary,welfare,address,collection,lng,lat')->from('yii_work')->where($where)->one();
         $list['city']=$this->GetRegion($list['city']);
         $list['region']=$this->GetRegion($list['region']);
         $list['experience']=$this->GetExperience($list['experience']);
         $list['education']=$this->GetEducation($list['education']);
         $list['welfare']=$this->GetWelfare($list['welfare']);
         $list['company']=$this->Company($list['company']);
         $list['address']=json_decode($list['address'],true);
              $token=\Yii::$app->request->get();
          if (isset($token['token'])){
              $user_id=$this->GetUser()['user_id'];
              $list['collection']=$this->iscollection($user_id,$id);
          }
         return json_encode($list);
     }


     //是否收藏
     public function iscollection($user_id,$work_id){
       $where['user_id']=$user_id;
       $where['work_id']=$work_id;
       $res=(new Query())->select('*')->from('yii_collection')->where($where)->one();
        if ($res){
           return 10;
         }else{
           return 20;
        }
     }

    //公司介绍
    public function Company($id){
        $id=(int)$id;
        $list=(new Query())->select('id,name,logo,industry,number,financing,desc,website,address')->from('yii_company',['id'=>$id])->one();
        $list['financing']=$this->GetFinancing($list['financing']);
        $list['number']=$this->GetNumber($list['number']);
        $list['industry']=$this->GetIndustry($list['industry']);
        $list['logo']=$this->GetImg($list['logo']);
        $list['image']=$this->GetCompanyImage($id);
        $list['work']=$this->GetWork($id);

        return $list;
    }




    //职业详情
    public function GetWorkInfo($work_id){
        $list=(new Query())->select('id,name,city,on,region,company,experience,education,min_salary,max_salary,welfare,collection')->from('yii_work')->where(['id'=>$work_id])->one();
        $list['city']=$this->GetRegion($list['city']);
        $list['region']=$this->GetRegion($list['region']);
        $list['experience']=$this->GetExperience($list['experience']);
        $list['education']=$this->GetEducation($list['education']);
        $list['welfare']=$this->GetWelfare($list['welfare']);
        $list['company']=$this->GetCompany($list['company']);
        return $list;
     }


     //投递简历
     public function actionDeliver(){
         $id=$this->PostId();
         //判断简历
         $where['user_id']=$this->GetUser()['user_id'];
         $userinfo=(new Query())->select('*')->from('yii_user')->where($where)->one();
         //$education=(new Query())->select('*')->from('yii_user_education')->where($where)->one();
         //$evaluate=(new Query())->select('*')->from('yii_user_evaluate')->where($where)->one();
         //$experience=(new Query())->select('*')->from('yii_user_experience')->where($where)->one();
         //$hope=(new Query())->select('*')->from('yii_user_hope')->where($where)->one();
         if(!is_null($userinfo['phone'])){
             //判断是否投递过
             $data=(new Query())->select('name')->from('yii_work')->one();
             $data['user_id']=$this->GetUser()['user_id'];
             $data['work_id']=$id;
             $check=(new Query())->select('*')->from('yii_user_work')->where($data)->one();
             if ($check){
                 return json_encode(['code'=>0,'msg'=>'你已经报过名']);
             }
             $data['create_time']=time();
             $res=\Yii::$app->db->createCommand()->insert('yii_user_work',$data)->execute();
             if ($res){
                 return json_encode(['code'=>1,'msg'=>'报名成功']);
             }else{
                 return json_encode(['code'=>0,'msg'=>'失败失败']);
             }
         }else{
             return json_encode(['code'=>0,'msg'=>'完善简历即可报名心仪职位']);
         }
     }

     //关键词搜索
    public function actionSearch(){
        $data['keyword']=\Yii::$app->request->post()['keyword'];

        $list=(new Query())->select('id,name,city,region,company,experience,education,min_salary,max_salary,welfare,collection')->from('yii_work')->where(['like','name',$data['keyword']])->all();
        foreach ($list as $k=>$v){
            $list[$k]['city']=$this->GetRegion($v['city']);
            $list[$k]['region']=$this->GetRegion($v['region']);
            $list[$k]['experience']=$this->GetExperience($v['experience']);
            $list[$k]['education']=$this->GetEducation($v['education']);
            $list[$k]['welfare']=$this->GetWelfare($v['welfare']);
            $list[$k]['company']=$this->GetCompany($v['company']);
        }
        if ($list){
            $data['keyword']=$list[0]['name'];
            $data['create_time']=time();
            \Yii::$app->db->createCommand()->insert('yii_keyword',$data)->execute();
        }
        return json_encode($list);
    }


    //热门关键词
    public function actionHotsearch(){
     $list=(new Query())->select('keyword')->from('yii_keyword')->all();
     $arr=[];
     foreach ($list as $v){
         array_push($arr,$v['keyword']);
     }


    $arr=array_count_values($arr);//统计数组中所有的值出现的次数
    arsort($arr);//倒序排序
    $data=[];
    foreach ($arr as $k=>$v){
     array_push($data,$k);
    }
    $arr=array_slice($arr,1,8);//截取前面8个
    return json_encode($data);
    }
}
